using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace IWS.SiteManager.Web.Controllers
{
	public class InstallController : Controller
	{
		public InstallController()
			: base()
		{
			this.SetMasterPage("_Install");
		}

		public ActionResult Index()
		{
			return View(new Core.Model.SetupInformation());
		}
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Index(FormCollection collection)
		{
			var info = new IWS.SiteManager.Core.Model.SetupInformation();

			info.DBType = collection["DBType"].Trim(); //*
			info.DBHost = collection["DBHost"].Trim(); //*
			info.DBName = collection["DBName"].Trim(); //*
			info.DBUser = collection["DBUser"].Trim(); //*
			info.DBPass = collection["DBPass"].Trim(); //*
			info.DBIntegrated = (collection["DBIntegrated"] == "true");
			info.MDBDifferent = collection["MDBDifferent"] == "true";
			info.MDBHost = collection["MDBHost"].Trim();
			info.MDBName = collection["MDBName"].Trim();
			info.MDBUser = collection["MDBUser"].Trim();
			info.MDBPass = collection["MDBPass"].Trim();
			info.MDBIntegrated = collection["MDBIntegrated"] == "true";

			if (ValidateDBInstall(info, collection))
			{
				try
				{
					//load up an object with the values. 
					new Core.InstallHelper().Install(info);
					return RedirectToAction("Step2");
				}
				catch (Exception ex)
				{
					ViewData["Exception"] = ex;
				}
			}

			return View(info);
		}
		
		public ActionResult Step2()
		{
			return View(new Core.Model.SetupInformation());
		}
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Step2(FormCollection collection)
		{
			var info = new Core.Model.SetupInformation();
			info.AdminUser = collection["AdminUser"].Trim(); //*
			info.AdminEmail = collection["AdminEmail"].Trim(); //*
			info.AdminPass = collection["AdminPass"].Trim(); //*

			if (ValidateAppSetup(info, collection))
			{
				try
				{
					//load up an object with the values. 
					new Core.InstallHelper().SetupApplication(info);
					return RedirectToAction("Complete");
				}
				catch (Exception ex)
				{
					ViewData["Exception"] = ex;
				}
			}

			return View(new Core.Model.SetupInformation());
		}

		public ActionResult Complete()
		{
			return View();
		}

		bool ValidateDBInstall(Core.Model.SetupInformation info, FormCollection collection)
		{

			if (!IsValidRequiredField(info.DBType))
				ModelState.AddModelError("DBType", "You must specify a database type.");
			if (!IsValidRequiredField(info.DBHost))
				ModelState.AddModelError("DBHost", "You must specify a database hostname.");
			if (!IsValidRequiredField(info.DBName))
				ModelState.AddModelError("DBName", "You must specify a database name.");

			if (!info.DBIntegrated)
			{
				if (!IsValidRequiredField(info.DBUser))
					ModelState.AddModelError("DBUser", "You must specify a database username.");
				if (!IsValidRequiredField(info.DBPass))
					ModelState.AddModelError("DBPass", "You must specify a database password.");
			}

			if (info.MDBDifferent && !info.MDBIntegrated)
			{
				if (!IsValidRequiredField(info.MDBUser))
					ModelState.AddModelError("MDBUser", "You must specify a membership database username.");
				if (!IsValidRequiredField(info.MDBPass))
					ModelState.AddModelError("MDBPass", "You must specify a membership database password.");
			}

			if (!ModelState.IsValid)
			{
				//rebind the fields

				//ModelState.SetModelValue("DBIntegrated", collection.ToValueProvider()["DBIntegrated"]);
				//ModelState.SetModelValue("MDBIntegrated", collection.ToValueProvider()["MDBIntegrated"]);
				ModelState.SetModelValue("DBType", collection.ToValueProvider()["DBType"]);
				ModelState.SetModelValue("DBHost", collection.ToValueProvider()["DBHost"]);
				ModelState.SetModelValue("DBName", collection.ToValueProvider()["DBName"]);
				ModelState.SetModelValue("DBUser", collection.ToValueProvider()["DBUser"]);
				ModelState.SetModelValue("DBPass", collection.ToValueProvider()["DBPass"]);
				//ModelState.SetModelValue("MDBDifferent", collection.ToValueProvider()["MDBDifferent"]);
				ModelState.SetModelValue("MDBHost", collection.ToValueProvider()["MDBHost"]);
				ModelState.SetModelValue("MDBName", collection.ToValueProvider()["MDBName"]);
				ModelState.SetModelValue("MDBUser", collection.ToValueProvider()["MDBUser"]);
				ModelState.SetModelValue("MDBPass", collection.ToValueProvider()["MDBPass"]);
			}

			return ModelState.IsValid;
		}
		bool ValidateAppSetup(Core.Model.SetupInformation info, FormCollection collection)
		{
			if (!IsValidRequiredField(info.AdminUser))
				ModelState.AddModelError("AdminUser", "You must specify an admin username.");
			if (!IsValidRequiredField(info.AdminPass))
				ModelState.AddModelError("AdminPass", "You must specify an admin password.");
			if (!IsValidRequiredField(info.AdminEmail))
				ModelState.AddModelError("AdminEmail", "You must specify an admin password.");
			if (!ModelState.IsValid)
			{
				//rebind the fields

				ModelState.SetModelValue("AdminUser", collection.ToValueProvider()["AdminUser"]);
				ModelState.SetModelValue("AdminPass", collection.ToValueProvider()["AdminPass"]);
				ModelState.SetModelValue("AdminEmail", collection.ToValueProvider()["AdminEmail"]);
			}

			return ModelState.IsValid;

		}


		bool IsValidRequiredField(string value)
		{
			return !string.IsNullOrEmpty(value);
		}

		void SetMasterPage(string section)
		{
			ViewData[Core.Constants.ViewDataMasterPageFile] = string.Format(Core.Constants.SiteMasterLocationFormatString, section);
		}
	}
}
